<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Services\CategoryService;
use App\Http\Requests\StoreCategoryRequest;
use App\Http\Requests\UpdateCategoryRequest;
use Illuminate\Http\Request;
use Inertia\Inertia;

/**
 * 分类管理控制器
 * 
 * 职责：
 * - 处理分类管理相关的HTTP请求
 * - 调用 CategoryService 处理业务逻辑
 * - 返回视图和重定向
 */
class CategoryController extends Controller
{
    /**
     * 分类服务
     * 
     * @var CategoryService
     */
    protected CategoryService $categoryService;

    /**
     * 构造器
     */
    public function __construct(CategoryService $categoryService)
    {
        $this->categoryService = $categoryService;
    }
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        // 调用服务层获取分类列表
        $categories = $this->categoryService->getList(
            $request->only(['search'])
        );

        return Inertia::render('Admin/Categories/Index', [
            'categories' => [
                'data' => $categories->items(),
                'meta' => [
                    'current_page' => $categories->currentPage(),
                    'from' => $categories->firstItem(),
                    'last_page' => $categories->lastPage(),
                    'links' => $categories->linkCollection()->toArray(),
                    'path' => $categories->path(),
                    'per_page' => $categories->perPage(),
                    'to' => $categories->lastItem(),
                    'total' => $categories->total(),
                ],
            ],
            'filters' => $request->only(['search'])
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return Inertia::render('Admin/Categories/Create');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreCategoryRequest $request)
    {
        // 验证已通过，调用服务层创建分类
        $this->categoryService->create($request->validated());

        return redirect()->route('admin.categories.index')
            ->with('message', '分类创建成功！');
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Category $category)
    {
        return Inertia::render('Admin/Categories/Edit', [
            'category' => $category
        ]);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateCategoryRequest $request, Category $category)
    {
        // 验证已通过，调用服务层更新分类
        $this->categoryService->update($category, $request->validated());

        return redirect()->route('admin.categories.index')
            ->with('message', '分类更新成功！');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Category $category)
    {
        try {
            // 调用服务层删除分类（包含业务规则检查）
            $this->categoryService->delete($category);

            return back()->with('message', '分类删除成功！');
        } catch (\App\Exceptions\BusinessException $e) {
            return back()->with('error', $e->getMessage());
        }
    }
}
